<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\common\model\AdvertCategoryModel;
use app\common\model\AdvertsModel;
use think\console\Input;
use think\facade\Db;
use think\facade\Session;

    /**
	 * Created by Ailcc.com
     * 2021-11-14 12:00:00 沛霖主页
     * 
	 */
class Adverts extends Common
{
    public function getList()
    {
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        $keyword = input('keyword');
        $category_id = input('category_id');

        if(!empty($keyword)){
            $this->comMap[] = ['title|id','like','%'.$keyword.'%'];
        }
        if(!empty($category_id)){
            $this->comMap[] = ['category_id','=',$category_id];
        }

        $list = AdvertsModel::pageList($this->comMap);
        return json(['code'=>0,'msg'=>'success','data'=>$list,'map'=>$this->comMap]);
    }

    public function del(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        $id = $this->request->param('ids');
        $thumb = $this->request->param('thumbs');
        unlink(".".$thumb);
        AdvertsModel::destroy($id);
        return json(['code'=>0,'msg'=>'删除成功']);
    }

	public function recommending(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
		$ids = $this->request->param('ids');
		$status = $this->request->param('status',1);
		AdvertsModel::recommending($ids,$status);
		return json(['code'=>0,'msg'=>'操作成功']);
	}

	public function topping(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
		$ids = $this->request->param('ids');
		$status = $this->request->param('status',1);
		AdvertsModel::topping($ids,$status);
		return json(['code'=>0,'msg'=>'操作成功']);
	}
    
    // 管理广告
    public function editShu() {
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }

        $userList = Db::name('Advert')->order('create_time DESC')->select();
        if ($this->request->isPost()) {
            $id = input('post.id');
            $action = input('post.action');
            $status = input('post.status');
            $value = input('post.value');
            if($value == 'true'){
                $value = '1';
            }elseif($value == 'false'){
                $value = '0';
                
            }
            // 更新留言信息
            Db::name('Advert')->where('id', $id)->update([$status => $value]);
            
            return json(['code'=>0,'msg'=>'操作成功']);
        }
        $this->getSide();
        return $this->fetch();
    }

    public function add(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        $data = $this->request->param('data');
        if(!empty($data['create_time'])){
            $data['create_time'] = strtotime($data['create_time']);
        }

        if(!empty($data['id'])){
            AdvertsModel::update($data);
        }else{
            unset($data['id']);
            $data['admin_id'] = session('admin_id');
            $Info = AdvertsModel::create($data);
        }
        return json(['code'=>0,'msg'=>'操作成功']);
    }

    /**
     * 获取分类列表
     */
    public function getCategoryList(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        $lang = input('lang');
        $AdvertCategory =  new AdvertCategoryModel();
        $list = $AdvertCategory->getFullList();
        foreach ($list as $key=>$val){
			if($lang=='en'){
				$list[$key]['name'] = $val['ename'];
			}
        }
        return json(['code'=>0,'msg'=>'操作成功','data'=>$list]);
    }

    /**
     * 添加分类
     */
    public function categoryAdd(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        $data = $this->request->param();
        if(empty($data['id'])){
            unset($data['id']);
            AdvertCategoryModel::create($data);
        }else{
            AdvertCategoryModel::update($data);
        }
        return json(['code'=>0,'msg'=>'操作成功']);
    }

    /**
     * 删除分类
     */
    public function categoryDel(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        $ids = $this->request->param('ids');
        AdvertCategoryModel::destroy($ids);
        return json(['code'=>0,'msg'=>'删除成功']);
    }


	/**
	 * 快捷按钮
	 */
    public function changeStatus(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
	    $id = $this->request->param('id');
	    $info = AdvertsModel::find($id);
	    if($info->status==1){
		    $info->status=0;
		    $msg = '已禁用';
	    }else{
		    $info->status=1;
		    $msg = '已启用';
	    }
	    $info->save();
	    return json(['code'=>0,'msg'=>$msg]);
    }

}
